#include "online.h"
#include "ui_online.h"
#include "tcpclient.h"

Online::Online(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Online)
{
    ui->setupUi(this);
}

Online::~Online()
{
    delete ui;
}

void Online::showUsr(PDU *pdu)
{
    if(NULL == pdu){
        return;
    }
    uint uiSize = pdu->uiMsgLen/CADATEMAX;
    char caTmp[CADATEMAX];
    for(uint i = 0; i < uiSize; i++){
        memcpy(caTmp,(char*)(pdu->caMsg) + i*CADATEMAX, CADATEMAX);
        QString strLoginName = TcpClient::getInstance().getloginName();
        if(strLoginName.toStdString().c_str() != caTmp){
            ui->online_lw->addItem(caTmp);
        }

    }
}

void Online::on_addFriend_clicked()
{
    QListWidgetItem *pItem = ui->online_lw->currentItem();
    QString strPerUsrName = pItem->text();
    QString strLoginName = TcpClient::getInstance().getloginName();
    PDU *pdu = mkPDU(0);
    pdu->uiMsgType = ENUM_MSG_TYPE_ADD_FRIEND_REQUEST;
    memcpy(pdu->caData, strPerUsrName.toStdString().c_str(),strPerUsrName.size());
    memcpy(pdu->caData + CADATEMAX, strLoginName.toStdString().c_str(), strLoginName.size());
    TcpClient::getInstance().getTcpSocket().write((char*)pdu,pdu->uiPDULen);
    free(pdu);
    pdu=NULL;
}

